package com.site_assembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.form.SiteForm;
import com.site_assembly.dao.SiteMapper;
import com.site_assembly.entity.Dcsite;
import com.site_assembly.entity.Site;
import com.site_assembly.service.ISiteService;
import com.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 子站点信息表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-09-20
 */
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements ISiteService {

    @Resource
    private SiteMapper siteMapper;

    @Override
    public Integer addSite(SiteForm siteForm) {

        Site site = new Site();
        BeanUtils.copyProperties(siteForm,site);

        return siteMapper.insert(site);
    }

    @Override
    public IPage findSiteByPageAndOperatingState(String operatingState, Integer current, Integer pageSize) {

        QueryWrapper<Site> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("operating_state",operatingState);
        IPage<Site> page = new Page<>(current,pageSize);

        return siteMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Integer setSiteOfOperatingStateBySiteNumber(String operatingState, String siteNumber) {

        UpdateWrapper<Site> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("operating_state",operatingState).eq("site_number",siteNumber);

        return siteMapper.update(null,updateWrapper);
    }

    @Override
    public Integer getSiteIdByUserId(Integer userId) {

        QueryWrapper<Site> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Site site = siteMapper.selectOne(queryWrapper);

        return site.getSiteId();
    }
}
